package com.zx.viewlearn.activity;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.zx.viewlearn.R;

/**
 * 用 xml 定义属性动画
 */
public class ViewLearnActivity4XML extends AppCompatActivity {
    ImageView my_iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_learn4_xml);
        my_iv = findViewById(R.id.my_iv);
    }

    public void valueAnimator1(View view) {
        ValueAnimator animator = (ValueAnimator) AnimatorInflater.loadAnimator(this, R.animator.value_anim_file);
        animator.setTarget(my_iv);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            /**
             * <p>Notifies the occurrence of another frame of the animation.</p>
             *
             * @param animation The animation which was repeated.
             */
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("ViewLearnActivity2", "value:" + value);
            }
        });
        animator.start();
    }

    public void objectAnimator1(View view) {
        ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.object_anim_file);
        animator.setTarget(my_iv);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            /**
             * <p>Notifies the occurrence of another frame of the animation.</p>
             *
             * @param animation The animation which was repeated.
             */
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.e("ViewLearnActivity2", "value:" + value);
            }
        });
        animator.start();

    }

    public void animatorSet(View view) {
        final Animator animator = AnimatorInflater.loadAnimator(this, R.animator.set_anim_file);
        animator.setTarget(my_iv);
        animator.addListener(new AnimatorListenerAdapter() {
            /**
             * {@inheritDoc}
             *
             * @param animation
             */
            @Override
            public void onAnimationEnd(Animator animation) {
                animator.start();


            }
        });
        animator.start();
    }
}
